<template>
    <div class="layout-padding">
        <el-card class="layout-padding-auto" shadow="hover">
            <div class="mb15">
                <el-input placeholder="请输入导航名称" size="default" v-model="state.tableData.param.name"
                          style="max-width: 180px"></el-input>
                <el-button class="ml10" size="default" type="primary" @click="getTableData">
                    <el-icon>
                        <ele-Search/>
                    </el-icon>
                    查询
                </el-button>
                <el-button class="ml10" size="default" type="success" @click="openAddDialog">
                    <el-icon>
                        <ele-FolderAdd/>
                    </el-icon>
                    新增
                </el-button>
            </div>
            <el-table v-loading="state.tableData.loading" :data="state.tableData.data" row-key="id" style="width: 100%">
                <el-table-column label="序号" type="index" width="60"/>
                <el-table-column label="名称" prop="name" show-overflow-tooltip></el-table-column>
                <el-table-column label="路径" prop="path" show-overflow-tooltip></el-table-column>
                <el-table-column label="打开方式" prop="openStyle" show-overflow-tooltip>
                    <template #default="{row}">
                        <span v-if="row.openStyle===0">当前窗口</span>
                        <span v-else-if="row.openStyle===1">新开窗口</span>
                        <span v-else>当前窗口</span>
                    </template>
                </el-table-column>
                <el-table-column label="排序权重" prop="sort" show-overflow-tooltip></el-table-column>
                <el-table-column label="简介" prop="introduction" show-overflow-tooltip></el-table-column>
                <el-table-column label="元数据" prop="metadata" show-overflow-tooltip></el-table-column>
                <el-table-column label="状态" prop="hide" show-overflow-tooltip>
                    <template #default="{row}">
                        <el-tag v-if="!row.hide" size="default" type="success">显示</el-tag>
                        <el-tag v-else size="default" type="warning">隐藏</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="创建时间" prop="createTime" min-width="90px"
                                 show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" width="160">
                    <template #default="{row}">
                        <el-button size="small" text type="primary"
                                   @click="openEditDialog(row)"
                        >修改
                        </el-button
                        >
                        <el-button size="small" text type="primary"
                                   @click="deleteNav(row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--            <el-pagination-->
            <!--                v-model:current-page="state.tableData.param.page"-->
            <!--                v-model:page-size="state.tableData.param.size"-->
            <!--                :page-sizes="[10, 20, 30]"-->
            <!--                :total="state.tableData.totalCount"-->
            <!--                background-->
            <!--                class="mt15"-->
            <!--                layout="total, sizes, prev, pager, next, jumper"-->
            <!--            >-->
            <!--            </el-pagination>-->
        </el-card>
        <add-nav ref="addNavRef" :navList="state.tableData.data" @refresh="getTableData()"/>
        <edit-nav ref="editNavRef" :navList="state.tableData.data" @refresh="getTableData()"/>
    </div>
</template>

<script lang="ts" name="navManagement" setup>
import {defineAsyncComponent, onMounted, reactive, ref, watch} from 'vue';
import {ElMessage, ElMessageBox} from 'element-plus';
import {navApi} from "/@/api/nav";

// 引入组件
const AddNav = defineAsyncComponent(() => import('./AddNav.vue'));
const EditNav = defineAsyncComponent(() => import('./EditNav.vue'));

// 定义变量内容
const addNavRef = ref();
const editNavRef = ref();

const state = reactive<NavState>({
    tableData: {
        data: [],
        totalCount: 0,
        loading: false,
        param: {
            page: 1,
            size: -1,//不分页
            name: "",
            parentId: 0
        },
    },
});

// 初始化表格数据
const getTableData = async () => {
    state.tableData.loading = true;
    const pageResult = await navApi.listNav(state.tableData.param);
    state.tableData.data = pageResult.records;
    state.tableData.totalCount = pageResult.totalCount;
    state.tableData.loading = false;
};
// 打开新增弹窗
const openAddDialog = () => {
    addNavRef.value.openDialog();
};
// 打开编辑弹窗
const openEditDialog = (row: NavVo) => {
    editNavRef.value.openDialog(row);
};

const deleteNav = (row: NavVo) => {
    ElMessageBox.confirm(`此操作将“${row.name}”删除，是否继续?`, '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    })
        .then(async () => {
            state.tableData.loading = true;
            await navApi.deleteNav(row.id)
            await getTableData();
            ElMessage.success('删除成功');
        }).finally(() => {
        state.tableData.loading = false;
    })
};
watch(() => state.tableData.param.page, () => {
    getTableData();
})
watch(() => state.tableData.param.size, () => {
    getTableData();
})
// 页面加载时
onMounted(() => {
    getTableData();
});
</script>

<style lang="scss" scoped>
.layout-padding {
    :deep(.el-card__body) {
        display: flex;
        flex-direction: column;
        flex: 1;
        overflow: auto;

        .el-table {
            flex: 1;
        }
    }
}
</style>
